//
//  AIGirlProfileGetTableViewCell.swift
//  AIGirl
//
//  Created by Rae on 2023/12/29.
//

import UIKit

class AIGirlProfileGetTableViewCell: UITableViewCell {

    private let backView = c_make_ex(UIImageView.self)
        .image("AIGirl_Profile_getBg").end()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    var dataSource: [String] = [String](){
        didSet{
            collectionView.reloadData()
        }
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                collectionView
            }
        }
        
        backView.leading(15).trailing(15).top(20).height(190)
        collectionView.leading(0).trailing(0).height(155).centerVertically()
        
        backView.isUserInteractionEnabled = true
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.itemSize = CGSize(width: 115.0, height: 155.0)
        layout.scrollDirection = .horizontal
        layout.sectionInset = UIEdgeInsets(top: 0, left: 15.0, bottom: 0, right: 10.0)
        collectionView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(AIGirlProfileGetItem.self, forCellWithReuseIdentifier: AIGirlProfileGetItem.description())
            v.delegate = self
            v.dataSource = self
            v.showsHorizontalScrollIndicator = false
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension AIGirlProfileGetTableViewCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: AIGirlProfileGetItem.description(), for: indexPath) as? AIGirlProfileGetItem else { return UICollectionViewCell() }
//        item.msgModel = dataSource[indexPath.row]
        item.getsUrl = dataSource[indexPath.row]
        return item
    }
}


class AIGirlProfileGetItem: UICollectionViewCell {
    
    private let backView = c_make_ex(UIImageView.self)
        .corner(40, corners: [.layerMaxXMinYCorner,.layerMinXMaxYCorner]).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(15, corners: [.layerMinXMinYCorner,.layerMaxXMaxYCorner]).end()
    private let selImg = c_make_ex(UIImageView.self)
        .image("AIGirl_Profile_getSel").mode(.scaleAspectFill).end()
    private let borderImg = c_make_ex(UIImageView.self)
        .image("AIGirl_Profile_bordorSel").mode(.scaleAspectFill).end()
    private let lockImageV = c_make_ex(UIImageView.self)
        .image("AIGirl_Message_lock").mode(.scaleAspectFill).end()
    private let crownImageV = c_make_ex(UIImageView.self)
        .image("AIGirl_Message_vip").end()
    private let diamondImageV = UIIconButton(style: .leading(itemSpace: 0))
//    private var blurView = c_make_ex(UIView.self).end()
    var blurEffectView:UIVisualEffectView?
    var msgModel: AIGirlDiscoverDetailPicModel? {
        didSet{
            guard let md = msgModel else { return }
//            selImg.isHidden = !md.isSel
//            borderImg.isHidden = !md.isSel
            lockImageV.isHidden = md.isLook
            crownImageV.isHidden = true
            diamondImageV.isHidden = true
            let orUrl = md.image
            let realUrl = String(orUrl.filter { $0 != "\t" })
            iconImageView.loadImage(url: realUrl)
            if !md.isLook {
                blurEffectView?.isHidden = false
                if md.lookPrice > 0 {
                    crownImageV.isHidden = true
                    diamondImageV.isHidden = false
                    diamondImageV.setTitle("\(md.lookPrice)", for: .normal)
                }else {
                    crownImageV.isHidden = false
                    diamondImageV.isHidden = true
                }
            }else {
                blurEffectView?.isHidden = true
            }
//            iconImageView.setImageWithBlur(fromURL: URL(string: md.image)!)
        }
    }
    
    var getsUrl: String?{
        didSet{
            guard let url = getsUrl else { return }
            let realUrl = String(url.filter { $0 != "\t" })
            iconImageView.loadImage(url: realUrl)
            lockImageV.isHidden = true
            crownImageV.isHidden = true
            diamondImageV.isHidden = true
            selImg.isHidden = true
            borderImg.isHidden = true
            blurEffectView?.isHidden = true
        }
    }
    
    func updateSelState(){
        guard let md = msgModel else { return }
        let selID = UserDefaultsManager.getInt(forKey: .bgPhototId)
        if selID == md.id {
            selImg.isHidden = false
            borderImg.isHidden = false
        }else{
            selImg.isHidden = true
            borderImg.isHidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .clear

        contentView.subviews {
            backView.subviews{
                iconImageView
                selImg
                borderImg
                lockImageV
                crownImageV
                diamondImageV
            }
            
        }

        backView.fillContainer()
        iconImageView.fillContainer()
        borderImg.fillContainer()
//        blurView.fillContainer()
        selImg.trailing(6).bottom(6).size(20)
        lockImageV.size(22).centerHorizontally().centerVertically(offset: -10)
        crownImageV.width(16).height(12).centerHorizontally()
        crownImageV.Top == lockImageV.Bottom + 2.5
        diamondImageV.centerHorizontally()
        diamondImageV.Top == lockImageV.Bottom + 2.5
        
        diamondImageV.style { v in
            v.setImage("AIGirl_Discover_Detail_diamond".toImage, for: .normal)
            v.setTitle("333", for: .normal)
            v.setTitleColor(.white, for: .normal)
            v.titleLabel?.font = 9.5.font_semiBold
            v.iconSize = CGSize(width: 13, height: 13)
            v.isUserInteractionEnabled = false
        }
        crownImageV.isHidden = true
        applyBlurEffect(to: iconImageView)
        iconImageView.contentMode = .scaleAspectFill
    }
    
    func applyBlurEffect(to imageView: UIImageView) {
        let blurEffect = UIBlurEffect(style: .light)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.frame = imageView.bounds
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.blurEffectView = blurEffectView
        imageView.addSubview(blurEffectView)
    }

    override func updateConstraintsIfNeeded() {
        super.updateConstraintsIfNeeded()
        layoutIfNeeded()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
